//
//  HJWeexViewController.m
//  HJShop
//
//  Created by apple on 2019/4/14.
//  Copyright © 2019 HJShop. All rights reserved.
//

#import "HJWeexViewController.h"
#import <WeexSDK/WeexSDK.h>

@interface HJWeexViewController ()
{
    WXSDKInstance *instance;
}
@end

@implementation HJWeexViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.moudleName = @"weex";
    self.view.backgroundColor = [UIColor greenColor];
    
    instance = [[WXSDKInstance alloc] init];
    instance.viewController = self;
    instance.frame = CGRectMake(0, 64, self.view.bounds.size.width, self.view.bounds.size.height - 64);
    WEAK_SELF(self)
    instance.onCreate = ^(UIView *view) {
        STRONG_SELF(self)
        view.backgroundColor = [UIColor whiteColor];
        [self.weexView removeFromSuperview];
        self.weexView = view;
     
        [self.view addSubview:view];
    };
    instance.onFailed = ^(NSError *error) {
        NSLog(@"error %@",error);
        //process failure, you could open an h5 web page instead or just show the error.
    };
    instance.renderFinish = ^ (UIView *view) {
        //process renderFinish
        NSLog(@"renderFinish %@",view);
    };
    NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"file://%@/bundlejs/%@.js", [NSBundle mainBundle].bundlePath, _jsPath]];
    NSString *randomURL = [NSString stringWithFormat:@"%@%@random=%d",url.absoluteString,url.query?@"&":@"?",arc4random()];
    NSLog(@"url %@",url);
    [instance renderWithURL:[NSURL URLWithString:randomURL] options:@{@"bundleUrl":[url absoluteString]} data:nil];
}

- (HJWeexViewController *)initWithUrl:(NSString *)path
{
    if (self = [super init]) {
        self.jsPath = path;
    }
    return self;
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

- (void)setMoudleinfo:(NSDictionary *)moudleinfo
{
    [super setMoudleinfo:moudleinfo];
    _jsPath = [moudleinfo objectForKey:@"js"];
}

@end
